<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>README.org</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta name="title" content="README.org"/>
<meta name="generator" content="Org-mode"/>
<meta name="generated" content="2014-01-27 一 至今"/>
<meta name="author" content="hujianxin"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<style type="text/css">
 <!--/*--><![CDATA[/*><!--*/
  html { font-family: Times, serif; font-size: 12pt; }
  .title  { text-align: center; }
  .todo   { color: red; }
  .done   { color: green; }
  .tag    { background-color: #add8e6; font-weight:normal }
  .target { }
  .timestamp { color: #bebebe; }
  .timestamp-kwd { color: #5f9ea0; }
  .right  {margin-left:auto; margin-right:0px;  text-align:right;}
  .left   {margin-left:0px;  margin-right:auto; text-align:left;}
  .center {margin-left:auto; margin-right:auto; text-align:center;}
  p.verse { margin-left: 3% }
  pre {
	border: 1pt solid #AEBDCC;
	background-color: #F3F5F7;
	padding: 5pt;
	font-family: courier, monospace;
        font-size: 90%;
        overflow:auto;
  }
  table { border-collapse: collapse; }
  td, th { vertical-align: top;  }
  th.right  { text-align:center;  }
  th.left   { text-align:center;   }
  th.center { text-align:center; }
  td.right  { text-align:right;  }
  td.left   { text-align:left;   }
  td.center { text-align:center; }
  dt { font-weight: bold; }
  div.figure { padding: 0.5em; }
  div.figure p { text-align: center; }
  div.inlinetask {
    padding:10px;
    border:2px solid gray;
    margin:10px;
    background: #ffffcc;
  }
  textarea { overflow-x: auto; }
  .linenr { font-size:smaller }
  .code-highlighted {background-color:#ffff00;}
  .org-info-js_info-navigation { border-style:none; }
  #org-info-js_console-label { font-size:10px; font-weight:bold;
                               white-space:nowrap; }
  .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
                                 font-weight:bold; }
  /*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart  The following is the entire license notice for the
JavaScript code in this tag.

Copyright (C) 2012-2013 Free Software Foundation, Inc.

The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.  The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.


@licend  The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
 function CodeHighlightOn(elem, id)
 {
   var target = document.getElementById(id);
   if(null != target) {
     elem.cacheClassElem = elem.className;
     elem.cacheClassTarget = target.className;
     target.className = "code-highlighted";
     elem.className   = "code-highlighted";
   }
 }
 function CodeHighlightOff(elem, id)
 {
   var target = document.getElementById(id);
   if(elem.cacheClassElem)
     elem.className = elem.cacheClassElem;
   if(elem.cacheClassTarget)
     target.className = elem.cacheClassTarget;
 }
/*]]>*///-->
</script>

</head>
<body>

<div id="preamble">

</div>

<div id="content">
<h1 class="title">README.org</h1>


<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 第六章：序列：字符串、列表和元组</a>
<ul>
<li><a href="#sec-1-1">1.1 字符串</a>
<ul>
<li><a href="#sec-1-1-1">1.1.1 操作符</a></li>
<li><a href="#sec-1-1-2">1.1.2 内建函数</a></li>
<li><a href="#sec-1-1-3">1.1.3 字符串内建函数</a></li>
<li><a href="#sec-1-1-4">1.1.4 字符串的删除</a></li>
<li><a href="#sec-1-1-5">1.1.5 只适用于字符串的操作符</a></li>
<li><a href="#sec-1-1-6">1.1.6 原始字符串：</a></li>
<li><a href="#sec-1-1-7">1.1.7 字符串的不变性</a></li>
<li><a href="#sec-1-1-8">1.1.8 <b>unicode</b></a></li>
<li><a href="#sec-1-1-9">1.1.9 与字符串相关的模块</a></li>
<li><a href="#sec-1-1-10">1.1.10 python不是一‘nul’结束的</a></li>
</ul>
</li>
<li><a href="#sec-1-2">1.2 列表</a>
<ul>
<li><a href="#sec-1-2-1">1.2.1 与字符串相同的性质</a></li>
<li><a href="#sec-1-2-2">1.2.2 列表解析</a></li>
<li><a href="#sec-1-2-3">1.2.3 内建函数</a></li>
<li><a href="#sec-1-2-4">1.2.4 列表的内建函数</a></li>
</ul>
</li>
<li><a href="#sec-1-3">1.3 元组</a>
<ul>
<li><a href="#sec-1-3-1">1.3.1 元组和列表的不同点</a></li>
<li><a href="#sec-1-3-2">1.3.2 元组不可变, 但是可用list() 转化为列表.</a></li>
<li><a href="#sec-1-3-3">1.3.3 元组不可变性中的可变部分.</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>

<div id="outline-container-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> 第六章：序列：字符串、列表和元组 &nbsp;&nbsp;&nbsp;<span class="tag"><span class="___">字符串</span>&nbsp;<span class="__">列表</span>&nbsp;<span class="__">元组</span></span></h2>
<div class="outline-text-2" id="text-1">


</div>

<div id="outline-container-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> 字符串</h3>
<div class="outline-text-3" id="text-1-1">


</div>

<div id="outline-container-1-1-1" class="outline-4">
<h4 id="sec-1-1-1"><span class="section-number-4">1.1.1</span> 操作符</h4>
<div class="outline-text-4" id="text-1-1-1">




<pre class="example">in, not in, +, *, 切片操作符：[], [:], [::]
</pre>

<ul>
<li>切片操作
</li>
</ul>




<pre class="example">&gt;&gt;&gt; string = "hello world!"
&gt;&gt;&gt; string[1]
'e'
&gt;&gt;&gt; string [1:3]
'el'
&gt;&gt;&gt; string[3:1]
''
&gt;&gt;&gt; string[-1:0]
''
&gt;&gt;&gt; string[0:-1]
'hello world'
&gt;&gt;&gt; '步进操作'
'\xe6\xad\xa5\xe8\xbf\x9b\xe6\x93\x8d\xe4\xbd\x9c'
&gt;&gt;&gt; string[::1]
'hello world!'
&gt;&gt;&gt; string[::2]
'hlowrd'
&gt;&gt;&gt; string[::3]
'hlwl'
&gt;&gt;&gt; string[::-1]
'!dlrow olleh'
&gt;&gt;&gt;
</pre>

<ul>
<li>in, not in操作符
</li>
</ul>




<pre class="example">&gt;&gt;&gt; string = 'hello world!'
&gt;&gt;&gt; 'hello' in string
True
&gt;&gt;&gt; 'hello' not in string
False
</pre>

<ul>
<li>string 模块
</li>
</ul>




<pre class="example">&gt;&gt;&gt; import string
&gt;&gt;&gt; string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
&gt;&gt;&gt; string.lowercase
'abcdefghijklmnopqrstuvwxyz'
&gt;&gt;&gt; string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
&gt;&gt;&gt; string.digits
'0123456789'

&gt;&gt;&gt; 'hello' + 'world!'
'helloworld!'
&gt;&gt;&gt; 'hello' + ' ' + 'world!'
'hello world!'
&gt;&gt;&gt; s = 'hello' + ' ' + 'world!'
&gt;&gt;&gt; string.upper(s[:3])
'HEL'
&gt;&gt;&gt; s
'hello world!'

&gt;&gt;&gt; s = string.Template('My name is ${name} and my age is ${age}')
&gt;&gt;&gt; print s.substitute(name='hujianxin', age=21)
My name is hujianxin and my age is 21
</pre>


</div>

</div>

<div id="outline-container-1-1-2" class="outline-4">
<h4 id="sec-1-1-2"><span class="section-number-4">1.1.2</span> 内建函数</h4>
<div class="outline-text-4" id="text-1-1-2">

<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup><col class="left" /><col class="left" />
</colgroup>
<thead>
<tr><th scope="col" class="left">类型转换</th><th scope="col" class="left">可操作</th></tr>
</thead>
<tbody>
<tr><td class="left">list()</td><td class="left">enumerate() (转换为字典序列)</td></tr>
<tr><td class="left">str()</td><td class="left">len()</td></tr>
<tr><td class="left">unicode()</td><td class="left">max()</td></tr>
<tr><td class="left">basestring()</td><td class="left">min()</td></tr>
<tr><td class="left">tuple()</td><td class="left">reversed()</td></tr>
<tr><td class="left">chr()</td><td class="left">sorted()</td></tr>
<tr><td class="left">ord()</td><td class="left">sum()</td></tr>
<tr><td class="left">unichr()</td><td class="left">zip()</td></tr>
</tbody>
</table>

<i>内建函数的使用</i>



<pre class="example">&gt;&gt;&gt; s = 'foobar'
&gt;&gt;&gt; for i, t in enumerate(s):
...     print i, t
...
0 f
1 o
2 o
3 b
4 a
5 r
&gt;&gt;&gt; s, t = 'foa', 'obr'
&gt;&gt;&gt; zip(s, t)
[('f', 'o'), ('o', 'b'), ('a', 'r')]
</pre>

</div>

</div>

<div id="outline-container-1-1-3" class="outline-4">
<h4 id="sec-1-1-3"><span class="section-number-4">1.1.3</span> 字符串内建函数</h4>
<div class="outline-text-4" id="text-1-1-3">

<p><i>太多了，用dir（）内建函数可以查看</i>
例如：
</p>


<pre class="example">&gt;&gt;&gt; dir('hello')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
&gt;&gt;&gt;
</pre>

</div>

</div>

<div id="outline-container-1-1-4" class="outline-4">
<h4 id="sec-1-1-4"><span class="section-number-4">1.1.4</span> 字符串的删除</h4>
<div class="outline-text-4" id="text-1-1-4">

<p>del string
</p></div>

</div>

<div id="outline-container-1-1-5" class="outline-4">
<h4 id="sec-1-1-5"><span class="section-number-4">1.1.5</span> 只适用于字符串的操作符</h4>
<div class="outline-text-4" id="text-1-1-5">

<ul>
<li>字符串格式化符号
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup><col class="left" /><col class="left" />
</colgroup>
<thead>
<tr><th scope="col" class="left">格式化字符</th><th scope="col" class="left">转换方式</th></tr>
</thead>
<tbody>
<tr><td class="left">%c</td><td class="left">字符</td></tr>
<tr><td class="left">%r</td><td class="left">repr（）优先处理的字符串</td></tr>
<tr><td class="left">%s</td><td class="left">str（）优先处理的字符串</td></tr>
<tr><td class="left">%d/%i</td><td class="left">有符号十进制数</td></tr>
<tr><td class="left">%u</td><td class="left">无符号十进制数</td></tr>
<tr><td class="left">%o</td><td class="left">无符号八进制数</td></tr>
<tr><td class="left">%x/%X</td><td class="left">无符号十六进制数</td></tr>
<tr><td class="left">%e/%E</td><td class="left">科学计数法</td></tr>
<tr><td class="left">%f/%F</td><td class="left">浮点数</td></tr>
<tr><td class="left">%g/%G</td><td class="left"></td></tr>
<tr><td class="left">%%</td><td class="left">输出%</td></tr>
</tbody>
</table>

</li>
<li>格式化操作符辅助指令
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup><col class="left" /><col class="left" />
</colgroup>
<thead>
<tr><th scope="col" class="left">符号</th><th scope="col" class="left">作用</th></tr>
</thead>
<tbody>
<tr><td class="left">*</td><td class="left">定义宽度或者小数点精度</td></tr>
<tr><td class="left">-</td><td class="left">左对齐</td></tr>
<tr><td class="left">+</td><td class="left">在正数前面显示+</td></tr>
<tr><td class="left">&lt;sp&gt;</td><td class="left">在整数前面显示空格</td></tr>
<tr><td class="left">#</td><td class="left">八进制前面显示0,十六进制前面显示0x</td></tr>
<tr><td class="left">0</td><td class="left">显示数字的前面显示0而不是空格</td></tr>
<tr><td class="left">%</td><td class="left">%</td></tr>
<tr><td class="left">(var)</td><td class="left">映射变量（字典参数）</td></tr>
<tr><td class="left">m.n</td><td class="left">m最小宽度，n小数点后面的位数</td></tr>
</tbody>
</table>

</li>
</ul>




<pre class="example">&gt;&gt;&gt; '%+d, %+d' % (1, -1)
'+1, -1'
&gt;&gt;&gt; '%#o, %#x' %(12, 12)
'014, 0xc'
&gt;&gt;&gt; '%03d' % 2
'002'
&gt;&gt;&gt; '%-3d' % 3
'3  '
</pre>

</div>

</div>

<div id="outline-container-1-1-6" class="outline-4">
<h4 id="sec-1-1-6"><span class="section-number-4">1.1.6</span> 原始字符串：</h4>
<div class="outline-text-4" id="text-1-1-6">

<p>用操作符 <b>r</b> 来表示
主要用途： <b>正则表达式</b> 。
<b>引入re模块来说用原始字符串的作用</b>
</p>


<pre class="example">&gt;&gt;&gt; import re
&gt;&gt;&gt; m = re.search('\\[n]', r'Hello \n')
&gt;&gt;&gt; m.group()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: 'NoneType' object has no attribute 'group'
&gt;&gt;&gt; m = re.search('\\\[n]', r'Hello \n')
&gt;&gt;&gt; m.group()
'\\n'
&gt;&gt;&gt; m = re.search(r'\\[n]', r'Hello \n')
&gt;&gt;&gt; m.group()
'\\n'
</pre>


</div>

</div>

<div id="outline-container-1-1-7" class="outline-4">
<h4 id="sec-1-1-7"><span class="section-number-4">1.1.7</span> 字符串的不变性</h4>
<div class="outline-text-4" id="text-1-1-7">

<p><i>直接上例子</i>
</p>


<pre class="example">&gt;&gt;&gt; s = 'hello'
&gt;&gt;&gt; s
'hello'
&gt;&gt;&gt; id(s)
3072956288L
&gt;&gt;&gt; s += 'world!'
&gt;&gt;&gt; s
'helloworld!'
&gt;&gt;&gt; id(s)
3072937632L
&gt;&gt;&gt; s[1]
'e'
&gt;&gt;&gt; s[1] = 'c'
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: 'str' object does not support item assignment
</pre>

</div>

</div>

<div id="outline-container-1-1-8" class="outline-4">
<h4 id="sec-1-1-8"><span class="section-number-4">1.1.8</span> <span class="todo TODO">TODO</span> <b>unicode</b></h4>
<div class="outline-text-4" id="text-1-1-8">

</div>

</div>

<div id="outline-container-1-1-9" class="outline-4">
<h4 id="sec-1-1-9"><span class="section-number-4">1.1.9</span> 与字符串相关的模块</h4>
<div class="outline-text-4" id="text-1-1-9">

<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup><col class="left" /><col class="left" />
</colgroup>
<thead>
<tr><th scope="col" class="left">模块</th><th scope="col" class="left">描述</th></tr>
</thead>
<tbody>
<tr><td class="left">string</td><td class="left"></td></tr>
<tr><td class="left">re</td><td class="left"></td></tr>
<tr><td class="left">struct</td><td class="left">字符串与二进制转换</td></tr>
<tr><td class="left">c/StringIO</td><td class="left">字符串缓冲对象，操作类似于file对象</td></tr>
<tr><td class="left">base26</td><td class="left">base 16 32 64 数据编解码</td></tr>
<tr><td class="left">codecs</td><td class="left">解码器注册和基类</td></tr>
<tr><td class="left">crypt</td><td class="left">进行单方面加密</td></tr>
<tr><td class="left">difflib</td><td class="left">找出序列间的不同</td></tr>
<tr><td class="left">hashlib</td><td class="left">各种不同安全hash算法和信息摘要算法的api</td></tr>
<tr><td class="left">hma</td><td class="left">HMAC信息鉴权算法的python实现</td></tr>
<tr><td class="left">md5</td><td class="left">RSA的md5信息摘要鉴权</td></tr>
<tr><td class="left">rotor</td><td class="left">提供多平台的加密解密服务</td></tr>
<tr><td class="left">sha</td><td class="left">NIAT的安全hash算法</td></tr>
<tr><td class="left">stringprep</td><td class="left">提供ip协议的unicode字符串</td></tr>
<tr><td class="left">textwrap</td><td class="left">文本包装和填充</td></tr>
<tr><td class="left">unicodedata</td><td class="left">unicode数据库</td></tr>
</tbody>
</table>


</div>

</div>

<div id="outline-container-1-1-10" class="outline-4">
<h4 id="sec-1-1-10"><span class="section-number-4">1.1.10</span> python不是一‘nul’结束的</h4>
<div class="outline-text-4" id="text-1-1-10">

<p>在字符串中，我们只需要关注自己的内容即可。其余的事情python已经解决了。
</p></div>
</div>

</div>

<div id="outline-container-1-2" class="outline-3">
<h3 id="sec-1-2"><span class="section-number-3">1.2</span> 列表</h3>
<div class="outline-text-3" id="text-1-2">


</div>

<div id="outline-container-1-2-1" class="outline-4">
<h4 id="sec-1-2-1"><span class="section-number-4">1.2.1</span> 与字符串相同的性质</h4>
<div class="outline-text-4" id="text-1-2-1">

<p>创建，访问（切片）， 删除，成员操作符（in, not in）, 链接符（+）
重复操作符（*）
</p></div>

</div>

<div id="outline-container-1-2-2" class="outline-4">
<h4 id="sec-1-2-2"><span class="section-number-4">1.2.2</span> 列表解析</h4>
<div class="outline-text-4" id="text-1-2-2">

<p><b>很重要，很实用。</b>
</p></div>

</div>

<div id="outline-container-1-2-3" class="outline-4">
<h4 id="sec-1-2-3"><span class="section-number-4">1.2.3</span> 内建函数</h4>
<div class="outline-text-4" id="text-1-2-3">

<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup><col class="left" /><col class="left" />
</colgroup>
<thead>
<tr><th scope="col" class="left">函数</th><th scope="col" class="left">作用</th></tr>
</thead>
<tbody>
<tr><td class="left">len()</td><td class="left"></td></tr>
<tr><td class="left">max()</td><td class="left"></td></tr>
<tr><td class="left">min()</td><td class="left"></td></tr>
<tr><td class="left">sorted()</td><td class="left"></td></tr>
<tr><td class="left">reserved()</td><td class="left"></td></tr>
<tr><td class="left">enumerate()</td><td class="left"></td></tr>
<tr><td class="left">zip()</td><td class="left"></td></tr>
<tr><td class="left">sum()</td><td class="left"></td></tr>
<tr><td class="left">list()</td><td class="left"></td></tr>
<tr><td class="left">tuple()</td><td class="left"></td></tr>
</tbody>
</table>


</div>

</div>

<div id="outline-container-1-2-4" class="outline-4">
<h4 id="sec-1-2-4"><span class="section-number-4">1.2.4</span> 列表的内建函数</h4>
<div class="outline-text-4" id="text-1-2-4">

<p>很多，可以用dir(list)观看。
</p></div>
</div>

</div>

<div id="outline-container-1-3" class="outline-3">
<h3 id="sec-1-3"><span class="section-number-3">1.3</span> 元组</h3>
<div class="outline-text-3" id="text-1-3">


</div>

<div id="outline-container-1-3-1" class="outline-4">
<h4 id="sec-1-3-1"><span class="section-number-4">1.3.1</span> 元组和列表的不同点</h4>
<div class="outline-text-4" id="text-1-3-1">

<ol>
<li>元组不可改变.
   这一点决定了元组可以干许多列表不可以干的事情. 例如:做一个字典的key. 还有就是处理一组对象是,这个组默认为元组.
</li>
<li>元组用(), 而列表用[].
</li>
</ol>

</div>

</div>

<div id="outline-container-1-3-2" class="outline-4">
<h4 id="sec-1-3-2"><span class="section-number-4">1.3.2</span> 元组不可变, 但是可用list() 转化为列表.</h4>
<div class="outline-text-4" id="text-1-3-2">

</div>

</div>

<div id="outline-container-1-3-3" class="outline-4">
<h4 id="sec-1-3-3"><span class="section-number-4">1.3.3</span> 元组不可变性中的可变部分.</h4>
<div class="outline-text-4" id="text-1-3-3">

<ol>
<li>重新赋值法.



<pre class="example">&gt;&gt;&gt; t = ('hello')
&gt;&gt;&gt; t
'hello'
&gt;&gt;&gt; t += ' world!'
&gt;&gt;&gt; t
'hello world!'
</pre>

</li>
<li>改变元组元素的元素法.



<pre class="example">&gt;&gt;&gt; t = (['hello', 3], [' world!'], 1)
&gt;&gt;&gt; t
(['hello', 3], [' world!'], 1)
&gt;&gt;&gt; t[0][1] = 2
&gt;&gt;&gt; t
(['hello', 2], [' world!'], 1)
</pre>

</li>
</ol>

</div>
</div>
</div>
</div>
</div>

<div id="postamble">
<p class="date">Date: 2014-01-27 一 至今</p>
<p class="author">Author: hujianxin</p>
<p class="creator"><a href="http://orgmode.org">Org</a> version 7.9.3f with <a href="http://www.gnu.org/software/emacs/">Emacs</a> version 24</p>
<a href="http://validator.w3.org/check?uri=referer">Validate XHTML 1.0</a>

</div>
</body>
</html>
